<?php

    require_once(dirname(__FILE__) . '/../includes/common.inc.php');

    define('PAGER_TPL', PATH_TEMPLATES . 'include/admin/pager.tpl');
    define('PAGE_SIZE', 10);

    $user = Core::App()->GetUser();

    $filters = Request::HasParam('filter') ? (array)Request::GetParam('filter') : array();

    if ( empty($filters[ArticleTable::FILTER_TYPE]) )
    {
        $filters[ArticleTable::FILTER_TYPE] = ArticleType::PRESS_RELEASE;
    }
    $typeSelected = $filters[ArticleTable::FILTER_TYPE];

    $categorySelected = isset($filters[ArticleTable::FILTER_CATEGORY]) ? $filters[ArticleTable::FILTER_CATEGORY] : null;
    $applyToSelected = isset($filters[ArticleTable::FILTER_PRODUCT]) ? $filters[ArticleTable::FILTER_PRODUCT] : null;
    if ( !isset( $filters[ArticleTable::FILTER_LANGUAGE] ) )
    {
        $filters[ArticleTable::FILTER_LANGUAGE] = SiteLanguage::EN;
    }
    $languageSelected = $filters[ArticleTable::FILTER_LANGUAGE];

    $user->SetState('articles_filter', $filters);

    $pageNumber = trim(Request::GetParam('page', 1));
    if ($pageNumber < 0)
    {
        $pageNumber = 1;
    }

    $user->SetState('articles_page', $pageNumber);

    $pager = ArticleTable::FindItems( $filters, $pageNumber, PAGE_SIZE );

    // redirect to first page if requested page is empty
    if ( $pageNumber > 1 && $pager->GetLastPage() < $pageNumber )
    {
        $params = array
        (
            'filter[category]' => $categorySelected,
            'filter[product]'  => $applyToSelected,
            'filter[language]' => $languageSelected,
            'page'             => 1
        );
        Request::Redirect( Pages::GetPageUrl( $pageInfo->id, $params ) );
    }

    Core::App()->GetUser()->SetReturnUrl(Request::GetURL());

    $articles = array();
    foreach ($pager->GetResults() as $item)
    {
        $itemProducts = ArticleTable::GetProducts($item->GetArticleId());
        $itemCategory = $item->GetCategoryId() ? ArticleCategoryTable::GetCategoryById( $item->GetCategoryId() ) : null;

        $article = array
        (
            'id'           => $item->GetArticleId(),
            'title'        => $item->GetTitle(),
            'is_published' => $item->GetIsPublished(),
            'category'     => $itemCategory ? $itemCategory->GetName() : '',
            'products'     => !empty($itemProducts) ? implode(', ', $itemProducts) : '',
            'language'     => CodeDriveSiteLanguage::GetLanguageName($item->GetLanguage()),
            'date'         => date('M j, Y', $item->GetCreateDate())
        );

        $articles[] = $article;
    }

    $applyToOptions = array
    (
        Product::CODEDRIVE             => Product::GetName(Product::CODEDRIVE),        
    );

    $page = Pages::CreatePage( $pageInfo->id );

    $page->SetVar('page', $pageNumber);
    $page->SetVar('pager', PagerNavigation::Generate( $pager, PAGER_TPL, 'articles.html?' ));
    $page->SetArrVar('type_options', Util::GetSelectOptions( ArticleType::GetTypes(), $typeSelected, false ));
    $page->SetArrVar('product_options', Util::GetSelectOptions( $applyToOptions, $applyToSelected, true, 'Any'));
    $page->SetArrVar('category_options', Util::GetSelectOptions( GetCategoryOptions(), $categorySelected, true, 'Any' ));
    $page->SetArrVar('language_options', Util::GetSelectOptions( CodeDriveSiteLanguage::GetSupportedLanguages(), $languageSelected, true, 'Any' ));
    $page->SetArrVar('articles', $articles);

    $page->Display();


    function GetCategoryOptions()
    {
        $categories = ArticleCategoryTable::GetCategories();
        $options = array();
        foreach ($categories as $categoryItem)
        {
            $options[$categoryItem->GetCategoryId()] = $categoryItem->GetName();
        }
        return $options;
    }
?>